package com.lebo.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.lebo.portal.constant.Constants;
import com.lebo.portal.model.LoginUser;

@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("{}",request.getSession().getMaxInactiveInterval());
        if(null == request.getSession().getAttribute(Constants.SESSION_ACCOUNT_LABLE)) {
            response.sendRedirect(request.getContextPath() + "/static/index.jsp");
            return false;
        }
        LoginUser account = (LoginUser) request.getSession().getAttribute(Constants.SESSION_ACCOUNT_LABLE);
        if(null == account) {
            response.sendRedirect(request.getContextPath() + "/static/index.jsp");
            return false;
        }
        logger.debug("LoginInterceptor...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
